<?php
declare (strict_types=1);

namespace app\model;

use app\lib\exception\BaseException;
use think\Model;

/**
 * @mixin \think\Model
 */
class Order extends Model
{
    const BOOKING = 1; //预约  or paypal
    const PROMPTLY = 2;   //即时 or 钱包

    const STATUS_START= 0; //待取件
    const STATUS_INIT = 10; //取件
    const STATUS_EXAMINE = 20; //签收
    const STATUS_FAIL = 30; //验货
    const STATUS_SUCCESS=50;//放款完成

    public static $status=[
        self::STATUS_START=>'待取件',
        self::STATUS_INIT=>'取件',
        self::STATUS_EXAMINE=>'签收',
        self::STATUS_FAIL=>'验机',
        self::STATUS_SUCCESS=>'成交收款'
    ];

    static public function getOrder($order,$user){

        $data=self::where('id',$order)->where('users_id',$user)
            ->with('detail')
            ->find();

       if(!$data) throw new BaseException(['msg'=>'订单参数异常']);

       return $data;
    }

    public function detail()
    {
        return $this->hasMany(OrderDetail::class, 'order_id', 'id');
    }

    public function getAddressAttr(string $name)
    {
        if(!empty($name)){
            return json_decode($name,true);
        }else{
            return $name;
        }
    }

    public function getTimeAttr($value)
    {
        if(!empty($value)){
            return $value;
        }
        return 'NO Date';
    }

}
